<template>
  <div class="cart py-container">
    <!--主内容-->

    <div class="checkout py-container pay">
      <div class="checkout-tit">
        <h4 class="fl tit-txt">
          <span class="success-icon"></span
          ><span class="success-info"
            >订单提交成功，请您及时付款！订单号：{{ payObj.out_trade_no }}</span
          >
        </h4>

        <span class="fr"
          ><em class="sui-lead">应付金额：</em
          ><em class="orange money">￥{{ payObj.total_fee }}</em></span
        >

        <div class="clearfix"></div>
      </div>

      <div class="checkout-steps">
        <div class="fl weixin">微信支付</div>

        <div class="fl sao">
          <p class="red">请使用微信扫一扫。</p>

          <div class="fl code">
            <!-- <img id="qrious" src="~/assets/img/erweima.png" alt=""> -->

            <!-- <qriously value="weixin://wxpay/bizpayurl?pr=R7tnDpZ" :size="338"/> -->

            <qriously :value="payObj.code_url" :size="200" />
            <div class="saosao">
              <p>请使用微信扫一扫</p>

              <p>扫描二维码支付</p>
            </div>
          </div>
        </div>

        <div class="clearfix"></div>

        <!-- <p><a href="pay.html" target="_blank">> 其他支付方式</a></p> -->
      </div>
    </div>
  </div>
</template>

<script>
import orderApi from "@/api/index/order";

export default {
  data() {
    return {
      orderNo: "",
      payObj: {},
      timer: null,
    };
  },
  created() {
    this.orderNo = this.$route.params.id;
    this.createQrCode();
  },
  mounted() {
    this.timer = setInterval(() => {
      //函数体
      //此时的orderNo是被封装为了 out_trade_no
      this.queryOrderState(this.payObj.out_trade_no);
    }, 3000);
  },

  beforeDestroy() {
    clearInterval(this.timer);
    this.timer = null;
  },
  methods: {
    //生成二维码
    createQrCode() {
      orderApi.createQrCode(this.orderNo).then((response) => {
        this.payObj = response.data.data;
      });
    },

    //设计定时器 查询订单状态
    queryOrderState(orderNo) {
      orderApi.getWxPayState(orderNo).then((response) => {
        if (response.data.success) {
          if (response.data.data.code == "2000") {
            clearInterval(this.timer); //如果支付监测成功则清除此定时器
            //提示信息
            this.$message({
              message: "恭喜你，支付成功！",
              type: "success",
            });
            this.$router.push({ path: "/course/" + this.payObj.course_id });
          }
          if (response.data.data.code == "2000") {
            return;
          }
        }
      });
    },
  },
};
</script>
